/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.dqhrm.service.impl;

import me.zhengjie.modules.dqhrm.domain.DqDoctors;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dqhrm.repository.DqDoctorsRepository;
import me.zhengjie.modules.dqhrm.service.DqDoctorsService;
import me.zhengjie.modules.dqhrm.service.dto.DqDoctorsDto;
import me.zhengjie.modules.dqhrm.service.dto.DqDoctorsQueryCriteria;
import me.zhengjie.modules.dqhrm.service.mapstruct.DqDoctorsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Kuki Wu
* @date 2024-12-06
**/
@Service
@RequiredArgsConstructor
public class DqDoctorsServiceImpl implements DqDoctorsService {

    private final DqDoctorsRepository dqDoctorsRepository;
    private final DqDoctorsMapper dqDoctorsMapper;

    @Override
    public PageResult<DqDoctorsDto> queryAll(DqDoctorsQueryCriteria criteria, Pageable pageable){
        Page<DqDoctors> page = dqDoctorsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dqDoctorsMapper::toDto));
    }

    @Override
    public List<DqDoctorsDto> queryAll(DqDoctorsQueryCriteria criteria){
        return dqDoctorsMapper.toDto(dqDoctorsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DqDoctorsDto findById(Integer id) {
        DqDoctors dqDoctors = dqDoctorsRepository.findById(id).orElseGet(DqDoctors::new);
        ValidationUtil.isNull(dqDoctors.getId(),"DqDoctors","id",id);
        return dqDoctorsMapper.toDto(dqDoctors);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(DqDoctors resources) {
        if(dqDoctorsRepository.findByPhoneNumber(resources.getPhoneNumber()) != null){
            throw new EntityExistException(DqDoctors.class,"phone_number",resources.getPhoneNumber());
        }
        if(dqDoctorsRepository.findByIdCardNumber(resources.getIdCardNumber()) != null){
            throw new EntityExistException(DqDoctors.class,"id_card_number",resources.getIdCardNumber());
        }
        dqDoctorsRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DqDoctors resources) {
        DqDoctors dqDoctors = dqDoctorsRepository.findById(resources.getId()).orElseGet(DqDoctors::new);
        ValidationUtil.isNull( dqDoctors.getId(),"DqDoctors","id",resources.getId());
        dqDoctors1 = dqDoctorsRepository.findByPhoneNumber(resources.getPhoneNumber());
        if(dqDoctors1 != null && !dqDoctors1.getId().equals(dqDoctors.getId())){
            throw new EntityExistException(DqDoctors.class,"phone_number",resources.getPhoneNumber());
        }
        dqDoctors1 = dqDoctorsRepository.findByIdCardNumber(resources.getIdCardNumber());
        if(dqDoctors1 != null && !dqDoctors1.getId().equals(dqDoctors.getId())){
            throw new EntityExistException(DqDoctors.class,"id_card_number",resources.getIdCardNumber());
        }
        dqDoctors.copy(resources);
        dqDoctorsRepository.save(dqDoctors);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            dqDoctorsRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DqDoctorsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DqDoctorsDto dqDoctors : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("姓名", dqDoctors.getRealName());
            map.put("昵称", dqDoctors.getNickName());
            map.put("微信名", dqDoctors.getWxName());
            map.put("微信号", dqDoctors.getWxNo());
            map.put("微信OpenID号", dqDoctors.getWxOpenid());
            map.put("电话号码", dqDoctors.getPhoneNumber());
            map.put("身份证号", dqDoctors.getIdCardNumber());
            map.put("本平台登录密码", dqDoctors.getDqHrmPassword());
            map.put("出生年月日", dqDoctors.getBirthDate());
            map.put("性别", dqDoctors.getGender());
            map.put("民族", dqDoctors.getEthnicity());
            map.put("身份证省份", dqDoctors.getIdCardProvince());
            map.put("身份证地址", dqDoctors.getIdCardAddress());
            map.put("电子邮箱", dqDoctors.getEmail());
            map.put("医师职称", dqDoctors.getPhysicianTitle());
            map.put("注册科室", dqDoctors.getRegisteredDepartment());
            map.put("医师职称注册范围", dqDoctors.getTitleRegistrationScope());
            map.put("医师职证称编号", dqDoctors.getTitleCertificateNumber());
            map.put("医师证执业证获取时间", dqDoctors.getPracticeCertificateObtainedDate());
            map.put("医师证原注册单位省份", dqDoctors.getOriginalRegistrationProvince());
            map.put("原注册单位城市", dqDoctors.getOriginalRegistrationCity());
            map.put("医师证原注册单位名称", dqDoctors.getOriginalRegistrationUnitName());
            map.put("医师证原注册单位级别", dqDoctors.getOriginalRegistrationUnitLevel());
            map.put("医师证医院性质", dqDoctors.getHospitalNature());
            map.put("医师资格证编号", dqDoctors.getQualificationCertificateNumber());
            map.put("资格证获取时间", dqDoctors.getQualificationObtainedDate());
            map.put("专业类别", dqDoctors.getSpecialtyCategory());
            map.put("毕业学校", dqDoctors.getGraduationSchool());
            map.put("学历", dqDoctors.getEducationLevel());
            map.put("医师执业证编号", dqDoctors.getPhysicianLicenseNumber());
            map.put("药师专业类别", dqDoctors.getPharmacistSpecialtyCategory());
            map.put("药师咨格证编号", dqDoctors.getPharmacistQualificationCertificateNumber());
            map.put("药师管理号", dqDoctors.getPharmacistManagementNumber());
            map.put("药师注册证编号", dqDoctors.getPharmacistRegistrationCertificateNumber());
            map.put("药师证签发单位", dqDoctors.getPharmacistIssuingUnit());
            map.put("药师执业类别", dqDoctors.getPharmacistPracticeCategory());
            map.put("药师执业范围", dqDoctors.getPharmacistPracticeScope());
            map.put("药师注册地址", dqDoctors.getPharmacistRegistrationAddress());
            map.put("个人简介", dqDoctors.getPersonalProfile());
            map.put("擅长科室及疾病用药", dqDoctors.getSpecialtiesAndDiseases());
            map.put("其他医疗相关证件", dqDoctors.getOtherMedicalCertificates());
            map.put("开户行", dqDoctors.getBankName());
            map.put("银行卡帐号", dqDoctors.getBankAccountNumber());
            map.put("总收入", dqDoctors.getTotalRevenue());
            map.put("等级评价", dqDoctors.getGrade());
            map.put("是否激活", dqDoctors.getIsEnable());
            map.put("审核状态", dqDoctors.getAuditStatus());
            map.put("审核结果信息", dqDoctors.getAuditResult());
            map.put("审核时间", dqDoctors.getAuditTime());
            map.put("注册时间", dqDoctors.getRegistrationTime());
            map.put("录入时间", dqDoctors.getCreateTime());
            map.put("录入人员", dqDoctors.getCreateUserId());
            map.put("修改时间", dqDoctors.getModifyTime());
            map.put("修改人员", dqDoctors.getModifyUserId());
            map.put("最后登录时间", dqDoctors.getLastLonginTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}